package itheima.zuoye.Test5;

import java.util.ArrayList;
import java.util.List;

import java.util.*;



public class DouDiZhu {
    public static void main(String[] args) {
        // 1. 创建四个 List 集合对象
        List<String> player1 = new ArrayList<>();
        List<String> player2 = new ArrayList<>();
        List<String> player3 = new ArrayList<>();
        List<String> diPai = new ArrayList<>();

        // 2. 初始化一副完整的扑克牌（54张）
        List<String> poker = createPoker();

        // 3. 打乱牌堆（洗牌）
        Collections.shuffle(poker);

        // 4. 发牌
        distributeCards(poker, player1, player2, player3, diPai);

        // 5. 抢地主功能
        int landlordIndex = chooseLandlord(player1, player2, player3);
        if (landlordIndex != -1) {
            // 地主获得底牌
            player1.addAll(diPai);
            System.out.println("玩家" + (landlordIndex + 1) + "成为地主，获得底牌：" + diPai);
        } else {
            System.out.println("没有人抢地主，重新发牌！");
            return;
        }

        // 6. 看牌（遍历集合）
        System.out.println("玩家1的牌：" + player1);
        System.out.println("玩家2的牌：" + player2);
        System.out.println("玩家3的牌：" + player3);
        System.out.println("底牌：" + diPai);

        // 7. 【选做】对每人手上的牌进行排序
        sortPoker(player1);
        sortPoker(player2);
        sortPoker(player3);

        System.out.println("排序后：");
        System.out.println("玩家1的牌：" + player1);
        System.out.println("玩家2的牌：" + player2);
        System.out.println("玩家3的牌：" + player3);
    }

    // 创建一副完整的扑克牌
    private static List<String> createPoker() {
        String[] suits = {"♠", "♥", "♣", "♦"};
        String[] ranks = {"3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A", "2"};
        List<String> poker = new ArrayList<>();

        for (String suit : suits) {
            for (String rank : ranks) {
                poker.add(suit + rank);
            }
        }
        poker.add("大王");
        poker.add("小王");

        return poker;
    }

    // 发牌
    private static void distributeCards(List<String> poker, List<String> player1, List<String> player2, List<String> player3, List<String> diPai) {
        for (int i = 0; i < 51; i++) {
            if (i % 3 == 0) {
                player1.add(poker.get(i));
            } else if (i % 3 == 1) {
                player2.add(poker.get(i));
            } else {
                player3.add(poker.get(i));
            }
        }
        for (int i = 51; i < 54; i++) {
            diPai.add(poker.get(i));
        }
    }

    // 抢地主功能
    private static int chooseLandlord(List<String> player1, List<String> player2, List<String> player3) {
        Scanner scanner = new Scanner(System.in);
        List<List<String>> players = Arrays.asList(player1, player2, player3);

        for (int i = 0; i < 3; i++) {
            System.out.println("玩家" + (i + 1) + "的牌：" + players.get(i));
            System.out.print("玩家" + (i + 1) + "是否要抢地主？(yes/no): ");
            String choice = scanner.nextLine();
            if ("yes".equalsIgnoreCase(choice)) {
                return i;
            }
        }

        return -1;
    }

    // 对牌进行排序
    private static void sortPoker(List<String> cards) {
        Collections.sort(cards, new Comparator<String>() {
            private final String order = "345678910JQKA2大王小王";

            @Override
            public int compare(String o1, String o2) {
                int index1 = order.indexOf(o1.replaceAll("[♠♥♣♦]", ""));
                int index2 = order.indexOf(o2.replaceAll("[♠♥♣♦]", ""));
                return Integer.compare(index1, index2);
            }
        });
    }
}


